*     *****************************************************************  * 
*     *****************************************************************  * 
*       File-Name:      individual.do                                    *  
*       Date:           13 June 2016                                     * 
*       Author:         Gschwend                                         * 
*       Purpose:      	individual analysis, Table 6.3 & 6.4, Figure 6.3 *
* 	    Input Files:    individual.dta                                   *
*       Data Output:    analysis.dta                                     *              
*     ****************************************************************   * 
*     ****************************************************************   * 



version 14.2
clear
capture log close
set more off



**********************************************
* Specify your own local path to the dropbox *
**********************************************



* Thomas G.'s local (office) path:
* local path /Users/gschwend/Dropbox/Andre/OUP Book

* Thomas G.'s local (Air) path:
* local path /Users/thomasgschwend/Dropbox/Andre/OUP Book
 




use individual, clear


* test of hypothesis 6

gen bavaria = (eid==202 | eid==212)


logit coalition importance Germany Spain if european!=1
estadd scalar LL = e(ll)
estat classification
estadd scalar PCP = r(P_corr)
eststo k1


logit coalition importance national Germany Spain if european!=1
estadd scalar LL = e(ll)
estat classification
estadd scalar PCP = r(P_corr)
eststo k2

logit coalition importance spid polknow national Germany Spain if european!=1
estadd scalar LL = e(ll)
estat classification
estadd scalar PCP = r(P_corr)
eststo k3




fre coalition if e(sample)
fre coalition





*****************
*** Table 6.4 ***
*****************


#delimit ;
esttab k1 k2 k3  using hyp6, b(%9.2f) se(%9.2f) 
 varlabels(_cons "Constant" sincere "Sincere Voting" european "EP election" regional "Regional" national "National Election"
          Germany "Germany" Spain "Spain" spid "Strength of PID" polknow "Pol. Knowledge"
		  non_viable "Non-viable Party" importance "Importance" non_viable_imp "Importance x non-viable Party")   noobs
		  title ("Table 6.4: Likelihood of Coalition Voting across National and Regional Elections")
 rtf replace nonotes addnote("N = `e(N)'; Standard errors in parentheses. * p < 0.05; ** p < 0.01")  star(* 0.05 ** 0.01) 
stats(LL PCP, fmt(%9.0f) ) 
varwidth(16)
order(importance  spid polknow national Germany Spain)
;
#delimit cr



reg importance coalition if european!=1
reg importance coalition 

*descriptive analysis that is reported
bysort coalition: sum importance if european!=1
bysort coalition: sum importance 

sum importance if european!=1
sum importance 






* test of hypothesis 3


logit sincere non_viable  Germany Spain
estadd scalar LL = e(ll)
estat classification
estadd scalar PCP = r(P_corr)
eststo k0


logit sincere non_viable importance non_viable_imp  Germany Spain
estadd scalar LL = e(ll)
estat classification
estadd scalar PCP = r(P_corr)
eststo k1

logit sincere non_viable importance non_viable_imp european Germany Spain
estadd scalar LL = e(ll)
estat classification
estadd scalar PCP = r(P_corr)
eststo k2

logit sincere non_viable importance non_viable_imp spid polknow european Germany Spain
estadd scalar LL = e(ll)
estat classification
estadd scalar PCP = r(P_corr)
eststo k3


fre sincere if e(sample)
fre sincere



*****************
*** Table 6.3 ***
*****************


#delimit ;
esttab k1 k2 k3  using hyp3, b(%9.2f) se(%9.2f) 
 varlabels(_cons "Constant" sincere "Sincere Voting" european "EP election" regional "Regional" national "National"
          Germany "Germany" Spain "Spain" spid "Strength of PID" polknow "Pol. Knowledge"
		  non_viable "Non-viable Party" importance "Importance" non_viable_imp "Importance x non-viable Party")  label noobs
		  title ("Table X.1: Likelihood of Sincere Voting across Election Studies")
 rtf replace nonotes addnote("N = `e(N)'; Standard errors in parentheses. * p < 0.05; ** p < 0.01")  star(* 0.05 ** 0.01) 
stats(LL PCP, fmt(%9.0f) ) 
modelwidth(10) 	
order(non_viable  importance non_viable_imp european spid polknow Germany Spain)
;
#delimit cr



**** Construct graph

logit sincere non_viable importance non_viable_imp spid polknow european Germany Spain
gen esample = (e(sample)==1)
keep if esample==1
keep sincere non_viable importance non_viable_imp spid polknow european Germany Spain
gen mergeid = _n	
compress								   /* get id variable to merge in the simulated coefficients */
save non_viable.dta, replace



sum mergeid									       /* get N to draw as many simulations as you have observation in the data */

drawnorm b1-b9, seed(1234) n(`r(N)') means(e(b)) cov(e(V)) clear
gen mergeid = _n                                   /* get id variable to merge in the simulated coefficients */

merge 1:1 mergeid using non_viable.dta
tab _merge
drop _merge
compress
save graph1.dta, replace
capture rm non_viable.dta                              /* clean-up! Get rid of data we do not need anymore */



tempname sim           // temporary filename, ensures that the file will be erased 
 
#delimit ;
* We need to postfile some variables in order to make a nice graph.                   *;
* In particular we need expected value of both scenarios & their confidence intervall *;
* If we wanna calculate first-differences we need to postfile it, too                 *;			 
postfile `sim' ev0 plo0 pup0  
               ev1 plo1 pup1 
               fd lod hid 
            using result, replace;

#delimit cr           
*     ******************************************************************************  *;
*       Start loop.  Let `a' be the values of the  key independent variable that is   *;
*  		represented  at the x-axis and let it run from 0,..., 10 in the desired      *;
* 		increments.                                                                   *;
*     ******************************************************************************  *;                                  


local a = 0 
while `a' <= 10 {       

* Thus we get 1 different values for `a' 

quietly {

*** Step 2: Define scenario  (setx-step) *;

* For varying importance we have two different values for educate (12 & 16) *;
* and we set everything else to its mean and mode)                               *;


* Scenario 1: non_viable==0 *;
#delimit ;
    gen x_beta0 =    b1*0
                   + b2*`a'
                   + b3*(`a'*0)
                   + b4*spid
                   + b5*polknow
	               + b6*european
                   + b7*Germany			   
                   + b8*Spain			   
				   + b9 ;
                            
* Scenario 2: non_viable==1*;
 
   gen x_beta1 =     b1*1
                   + b2*`a'
                   + b3*(`a'*1)
                   + b4*spid
                   + b5*polknow
	               + b6*european
                   + b7*Germany			   
                   + b8*Spain			   
				   + b9 ;  

#delimit cr 
  
*** Step 3: Simulate quantities of interest (simqi-step)  *;
  
                            
* Predicted probabilities given a logit model  *;
* P(turnout = 1) = exp(x_beta)/(1+exp(x_beta)) *;
* If you use a different model this will change *;

    gen prob0=exp(x_beta0)/(1+exp(x_beta0))	/* Predicted Probs Scenario 1*/
    gen prob1=exp(x_beta1)/(1+exp(x_beta1))	/* Predicted Probs Scenario 2*/
	
	*  gen prob16 = normalden(x_beta16) if were to estimate a probit model

    egen ev0 = mean(prob0) /*Expected value is the mean over all  pred probs for scenario 1*/
    egen ev1 = mean(prob1) /*Expected value is the mean over all  pred probs for scenario 2*/

    scalar prob_hat0 = ev0 /* For each value of `a' this is the value we postfile in scenario 1*/
    scalar prob_hat1 = ev1 /* For each value of `a' this is the value we postfile in scenario 2*/		

* In case we are interested in a first-difference of those scenarios: *;
    gen diff = prob1-prob0  
    egen  fd = mean(diff)
 
    
    scalar diff_hat = fd /* For each value of `a' this is the value we postfile as first-difference*/
 
* lower & upper bound for expected value of scenario 1 (95% CI)*;
    _pctile prob0, p(2.5,97.5) 
    scalar lo0 = r(r1)
    scalar hi0 = r(r2)  
	
* lower & upper bound for expected value of scenario 2	(95% CI) *;
    _pctile prob1, p(2.5,97.5) 
    scalar lo1 = r(r1)
    scalar hi1 = r(r2)  
	
* lower & upper bound for first-difference	(95% CI)*;
	_pctile diff, p(2.5,97.5) 
    scalar lod = r(r1)
    scalar hid = r(r2) 
 	

#delimit ;   
    post `sim'  (prob_hat0) (lo0) (hi0)  /* EV given scenario 1 and its 95% CI */
                (prob_hat1) (lo1) (hi1)  /* EV given scenario 2 and its 95% CI */
                (diff_hat) (lod) (hid);     /* FD of scenario 1-2 and its 95% CI */
#delimit cr
	}     
    drop x_beta0 x_beta1 prob0 prob1 diff fd ev0 ev1 /* will be generated anew for the next value of `a'*/
    local a=`a'+ 1 		/* increase counter +1 and repeat calculations */
	display "Simulating `a'"
 } 

postclose `sim'

* Now we can create a plot using our simulated results
use result, clear


* generate x-axis of plot *;
generate xaxis = _n - 1 in 1/11                 /* contains 0, .., 10 */

                 /* This defines the typical look of a rug */

merge 1:1 _n using graph1, keepusing(importance) 
drop _merge

gen fd1 = (lod + hid)/2 

/*
sysuse auto, clear
twoway hist mpg, yscale(alt axis(1)) ///
|| line weight mpg, sort yaxis(2)    ///
yscale( alt axis(2))


|| (hist  importance, discrete bcolor(gs14))
*/




******************
*** Figure 6.3 ***
******************


#delimit ;
graph twoway (hist  importance, barw(.25) bcolor(gs15) yaxis(2) )
|| (rarea lod hid xaxis, sort bcolor(gs13) )
|| (line fd1 xaxis, sort)
||, 
xlabel(0(2)10, nogrid  angle(horizontal))
ylabel(-.24 (0.06) .06, nogrid  angle(horizontal)  )
yscale(alt)
yscale(off axis(2))
	legend(off)       
	scheme(s1mono)  yscale(nofextend)   xscale(nofextend) 
	xtitle("Perceived Importance of Election", color(black) alignment(bottom )) xscale(titlegap(3) nofextend)    
	ytitle("Pr (Sincere Vote)" "if preferred Party is non-viable rather than viable") 
	graphregion(fcolor(white)) 
	plotregion(lcolor(white))		/* get rid of box (give it the same color as the background) except the axes */
name(sincere_indi, replace);
#delimit cr



graph export sincere_indi.tif, replace
graph export sincere_indi.pdf, replace
graph export sincere_indi.png, replace
graph export sincere_indi.eps, replace preview(on)


exit



